package cn.mrcode.fd.blog.services.impl;

import cn.mrcode.fd.blog.dao.mapper.LoginInfoMapper;
import cn.mrcode.fd.blog.dao.mapper.UserMapper;
import cn.mrcode.fd.blog.dao.mappercu.LoginInfoCuMapper;
import cn.mrcode.fd.blog.dao.mappercu.UserCuMapper;
import cn.mrcode.fd.blog.entity.LoginInfo;
import cn.mrcode.fd.blog.entity.User;
import cn.mrcode.fd.blog.entityCu.BlogConfigCu;
import cn.mrcode.fd.blog.entityCu.UserCu;
import cn.mrcode.fd.blog.services.IBlogConfigService;
import cn.mrcode.fd.blog.services.IUserService;
import cn.mrcode.fd.utils.mybatis.page.Page;
import cn.mrcode.fd.utils.mybatis.page.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户服务
 *
 * @author : zhuqiang
 * @version : V1.0
 * @date : 2015/12/26 23:38
 */
@Service
public class UserServiceImpl implements IUserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserCuMapper userCuMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Autowired
    private LoginInfoCuMapper loginInfoCuMapper;
    @Autowired
    private IBlogConfigService iBlogConfigService;

    @Override
    public User find(String account) {
        return userCuMapper.selectByAccount(account);
    }
    @Override
    public UserCu findUserCu(String account) {
        UserCu userCu =  userCuMapper.selectByAccount(account);
        return userCu;
    }

    @Override
    public List<UserCu> getRecUser(int num) throws Exception{
        Page<UserCu> page = new Page<>();
        page.setPageSize(num);
        PageHelper.setLocalPage(page);
        List<UserCu> users = userCuMapper.selectRecUser();
        return users;
    }

    @Override
    public List<UserCu> getAllUser(Page<UserCu> page) throws Exception{
        PageHelper.setLocalPage(page);
        List<UserCu> users = userCuMapper.selectRecUser();
        return users;
    }

    @Override
    @Transactional
    public void modProfile(String id, String name, String sign) {
        User user = new User();
        user.setSign(sign);
        user.setId(id);
        user.setName(name);
        userMapper.updateByPrimaryKeySelective(user);
        LoginInfo record = new LoginInfo();
        record.setUserId(id);
        record.setName(name);
        loginInfoCuMapper.updateByUserIdSelective(record);
    }

    @Override
    @Transactional
    public void modByKeySelective(User record) {
        userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public UserCu findUserConfig(String account) throws Exception {
        UserCu userCu = this.findUserCu(account);
        if(userCu != null){
            BlogConfigCu blogConfigCu = iBlogConfigService.findBlogConfigCu(userCu.getId());
            userCu.setBlogConfig(blogConfigCu);
        }
        return userCu;
    }
}
